import { action, observable } from "mobx";
// import window from '../../util';
import { initDetailExcelFile, detailImportNew ,confirmTemplate} from '../../apis/card';
import _ from 'lodash';
import { WeaLocaleProvider,WeaTools } from "ecCom";
import { message, Modal } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;

export default class DetailImportStore {
    @observable visible: boolean = false;
    @observable loading: boolean = false;
    // @observable importUrl: string = '/api/cube/card/detailImport';
    @observable importUrl: string = "/api/cube/mode/mode/uploadTempFile"//上传到临时文件目录

    @observable uploadParams: any = {};
    @observable modename: string = '';
    @observable selectedFile: string = "";
    @observable random: number = 0;
    startUploadAll: Function;
    initCard: Function;
    @observable applyToModel = '';
    @observable canUpload: boolean = false;
    constructor() {
        this.selectedFile = getLabel(384040, '未选择任何文件');
    }

    @action
    show = () => {
        initDetailExcelFile(this.uploadParams).then(({applyToModel})=>{
            this.applyToModel = applyToModel;
        })
        this.visible = true;
    }
    @action
    hide = () => {
        this.visible = false;
        this.uploadParams.isImportDetail= false;
    }
    @action
    onCancel = () => {
        this.hide();
    }
    @action
    onOk = () => {
        this.loading = true;
        // window.startUploadAll();
        //解决滑动弹出ifram窗口中，startUploadAll()未定义的问题。 使用下面方法对window做兼容处理
        const window = WeaTools.WindowFunc.getTop();
        window.startUploadAll();
    }

    @action
    uploading = (status: string) => {
        // if (status == 'uploaded') {
        //     this.loading = false;
        //     this.visible = false;
        //     this.initCard();
        //     this.random = new Date().getTime();
        // }
    }

    @action
    uploadChange = (ids, list) => {
        const that = this;
        if (ids && ids.length == 1) {
            let excelid = ids[0];
            const params = {
                ...this.uploadParams,
                excelid: excelid
            }
            detailImportNew(params).then((data: any) => {
                if (data.status == "1") {
                    if(data.istrue == "1"){
                        this.visible = false;
                        this.initCard();
                    }else{
                        Modal.error({
                            title: getLabel(24945, "导入失败"),
                            width:500,
                            content:<div style={{maxHeight:600,overflowY:'auto'}}><div dangerouslySetInnerHTML={{ __html: data.msg }}></div></div>,
                            onOk() {
                                that.visible = false;
                                that.initCard();
                            },
                        });
                    }
                } 
                this.loading = false;
                this.random = new Date().getTime();
            })
        } else {
            message.error("Excel" + getLabel(25389, "上传失败") + "！");
        }
    }

    @action
    getShowListDatas = (__: any, newData: any[]) => {
        if (newData && newData.length == 1) {
            this.selectedFile = _.get(newData, '[0].name');
            this.canUpload = true;
        } else {
            this.selectedFile = getLabel(384040, '未选择任何文件');
            this.canUpload = false;
        }
    }

    @action
    downloadTemplate = (params) =>{
        let {layoutid,formId,modeid,cardtype,billid} = params
        const href = `/weaver/weaver.formmode.data.FileDownload?type=detailimport_excel&layoutid=${ layoutid}&formId=${formId}&modeid=${ modeid}&cardtype=${cardtype}&billid=${billid} `;
        confirmTemplate(params).then((data: any) => {
            if (data.status == "1") {
                if(data.isHas == "1"){
                    window.open(href, "_self")
                }else{
                    message.error("Excel" + getLabel(520554, "模板不存在") + "！");
                }
            } 
            this.loading = false;
            this.random = new Date().getTime();
        })
      
      
    }
}